package javango.contrib.i18n;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.IllegalFormatException;
import java.util.Map;
import java.util.MissingFormatArgumentException;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;


public class I18NStringImpl implements I18NString {
	protected final String string;
	protected final Object[] args;
	protected final I18NProvider provider;
	
	protected Map<String, Object> formatParams;
	protected Object formatObject;
	Map<String, Object> map;
	
	public I18NStringImpl(I18NProvider provider, String string, Object[] args) {
		this.provider = provider;
		this.string = string;
		this.args = args;
	}

	@Override
	public String toString() {
		if (formatParams != null) {
			return provider.getText(string, formatParams);
		} else if (formatObject != null) {
			return provider.getText(string, (Object)formatObject);
		} else if (this.map != null) {
			return provider.getText(string, map);
		} else {
			return provider.getText(string, args);
		}
	}

	public I18NString with(String parameter, Object value) {
		formatParams = formatParams == null ? new HashMap<String, Object>() : formatParams;
		formatParams.put(parameter, value);
		return this;
	}

	public I18NString with(Object value) {
		formatObject = value;
		return this;
	}

	public I18NString withMap(Map<String, Object> map) {
		this.map = map;
		return this;
	}

}
